/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Entidades.Cotizacion;
import Entidades.Organo;
import Entidades.SolicitudTraslado;
import Servicios.Dao.CotizacionDao;
import Servicios.Dao.Impl.CotizacionDaoImpl;
import Servicios.Dao.Impl.OrganoDaoImpl;
import Servicios.Dao.Impl.SolicitudTrasladoDaoImpl;
import Servicios.Dao.OrganoDao;
import Servicios.Dao.SolicitudTrasladoDao;
import java.sql.Connection;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author TRACY
 */
public class Generar_Cotizacion extends javax.swing.JFrame {

     public Connection cn;
     CotizacionDao cotizacionDao;
     OrganoDao organoDao;
     SolicitudTrasladoDao solicitudTrasladoDao;
     Cotizacion cotizacion ;
     String idOrgano, idSolicitud;
     String cad ="";
     /**
     * Creates new form Generar_Cotizacion
     */
    public Generar_Cotizacion(Connection cn) {
        this.cn =cn;
        cotizacionDao = new CotizacionDaoImpl(cn);
        organoDao = new OrganoDaoImpl(cn);
        solicitudTrasladoDao = new SolicitudTrasladoDaoImpl(cn);
        cotizacion = new Cotizacion();
        initComponents();
    }
    
    public Generar_Cotizacion(String idOrgano, String idSolicitud, Connection cn) {
        this.cn =cn;
        this.idOrgano = idOrgano;
        this.idSolicitud = idSolicitud;
        cotizacionDao = new CotizacionDaoImpl(cn);
        organoDao = new OrganoDaoImpl(cn);
        solicitudTrasladoDao = new SolicitudTrasladoDaoImpl(cn);
        cotizacion = new Cotizacion();
        initComponents();
        AsignarIDCotización();
        LlenarCampos();
    }

    public void AsignarIDCotización(){              
       if (cotizacionDao.calcularMaxIDSol().equals("")) {
            cad ="CT0001";
        } else {
            int numero = Integer.parseInt(cotizacionDao.calcularMaxIDSol().substring(3))+1;
            String cadena= String.valueOf(numero);
            while (cadena.length()<4) {            
                cadena="0"+cadena;
            }
            cadena="CT"+cadena;       
            cad = cadena;
        }
       txtCodigo.setText(cad);
       cotizacion.setIDCotizacion(cad);
        
    }
    
    public void LlenarCampos(){
        Organo organo = organoDao.buscarOrganoxID(idOrgano);
        System.out.println("ID: "+idSolicitud);
        SolicitudTraslado st = solicitudTrasladoDao.buscarSolicitud(idSolicitud);
        txtSede.setText(organo.getNombreOrgano());
        txtDireccion.setText(organo.getDireccion());
        txtDistrito.setText(organo.getDistrito());
        txtMonto.setText(String.valueOf(st.getMontoSolcitado()));
        txtProvincia.setText(organo.getProvincia());
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSede = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtMonto = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtfecha = new com.toedter.calendar.JDateChooser();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtProvincia = new javax.swing.JLabel();
        txtDistrito = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("COTIZACIÓN");

        jLabel2.setText("Destino:");

        txtSede.setText("Sede Pueblo Libre");

        jLabel4.setText("Direccion:");

        txtDireccion.setText("Av. Bolivar 605 ");

        jLabel6.setText("Monto:");

        txtMonto.setText("20000");

        jLabel8.setText("Fecha de Traslado:");

        jLabel10.setText("Provincia:");

        jLabel11.setText("Distrito:");

        txtProvincia.setText("Lima");

        txtDistrito.setText("Pueblo Libre");

        jButton4.setText("Cancelar");

        jButton5.setText("Atras");

        jButton1.setText("Enviar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel14.setText("CODIGO:");

        txtCodigo.setText("jLabel15");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(151, 151, 151)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel14)
                                .addGap(18, 18, 18)
                                .addComponent(txtCodigo))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(txtSede))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtProvincia))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addGap(18, 18, 18)
                                .addComponent(txtDistrito))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtMonto, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtDireccion)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton5)
                                .addGap(77, 77, 77)
                                .addComponent(jButton1)
                                .addGap(40, 40, 40)
                                .addComponent(jButton4)))
                        .addGap(0, 100, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(txtCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSede))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtProvincia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtDistrito))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDireccion))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtMonto))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8))
                    .addComponent(txtfecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(69, 69, 69)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton5)
                    .addComponent(jButton1))
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Calendar Cal= Calendar.getInstance();
        Date ss = Cal.getTime();
        String fec= Cal.get(Cal.YEAR)+"-"+(Cal.get(Cal.MONTH)+1)+"-"+Cal.get(Cal.DATE);
        cotizacion.setFechaEmision(ss);
        cotizacion.setFechaTraslado(txtfecha.getDate());
        cotizacion.setIDSolicitud(idSolicitud);
        cotizacionDao.createCotizacion(cotizacion);
        JOptionPane.showMessageDialog(null, "Cotizacion se envio satisfactoriamente");
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel txtCodigo;
    private javax.swing.JLabel txtDireccion;
    private javax.swing.JLabel txtDistrito;
    private javax.swing.JLabel txtMonto;
    private javax.swing.JLabel txtProvincia;
    private javax.swing.JLabel txtSede;
    private com.toedter.calendar.JDateChooser txtfecha;
    // End of variables declaration//GEN-END:variables
}
